using System;
using System.Text;
using System.Windows.Forms;
using ctsu.Admin.BusLogic.CoreLab;

namespace ctsu.Admin.Forms.CoreLab
{
	public class NewFromExistingInEditDlg : System.Windows.Forms.Form
	{

		public delegate void ButtonClickEventHandler(object sender, EditBatchEventArgs e);
		public event ButtonClickEventHandler ButtonClick;
		private BLFormNewFromExistingIn _bl;

		#region controls
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.CheckBox chkAll;
		private System.Windows.Forms.CheckBox chk3;
		private System.Windows.Forms.CheckBox chk2;
		private System.Windows.Forms.CheckBox chk4;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnReplaceAll;
		private System.Windows.Forms.Button btnReplaceAllMatch;
		private System.Windows.Forms.Button btnFindNext;
		private System.Windows.Forms.Button btnReplaceSelected;
		private System.Windows.Forms.CheckBox chk5;
		private System.Windows.Forms.CheckBox chk6;
		private System.ComponentModel.Container components = null;
		#endregion controls

		public NewFromExistingInEditDlg(BLFormNewFromExistingIn bl)
		{
			InitializeComponent();
			_bl = bl;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.chk3 = new System.Windows.Forms.CheckBox();
			this.chk2 = new System.Windows.Forms.CheckBox();
			this.chk5 = new System.Windows.Forms.CheckBox();
			this.chkAll = new System.Windows.Forms.CheckBox();
			this.chk4 = new System.Windows.Forms.CheckBox();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnReplaceAll = new System.Windows.Forms.Button();
			this.btnReplaceAllMatch = new System.Windows.Forms.Button();
			this.btnFindNext = new System.Windows.Forms.Button();
			this.btnReplaceSelected = new System.Windows.Forms.Button();
			this.chk6 = new System.Windows.Forms.CheckBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.chk6);
			this.groupBox1.Controls.Add(this.chk3);
			this.groupBox1.Controls.Add(this.chk2);
			this.groupBox1.Controls.Add(this.chk5);
			this.groupBox1.Controls.Add(this.chkAll);
			this.groupBox1.Controls.Add(this.chk4);
			this.groupBox1.Location = new System.Drawing.Point(8, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(112, 152);
			this.groupBox1.TabIndex = 38;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Replace Columns";
			// 
			// chk3
			// 
			this.chk3.Location = new System.Drawing.Point(7, 60);
			this.chk3.Name = "chk3";
			this.chk3.Size = new System.Drawing.Size(88, 16);
			this.chk3.TabIndex = 14;
			this.chk3.Text = "Units";
			// 
			// chk2
			// 
			this.chk2.Location = new System.Drawing.Point(7, 38);
			this.chk2.Name = "chk2";
			this.chk2.Size = new System.Drawing.Size(88, 16);
			this.chk2.TabIndex = 13;
			this.chk2.Text = "Test Type";
			// 
			// chk5
			// 
			this.chk5.Location = new System.Drawing.Point(7, 104);
			this.chk5.Name = "chk5";
			this.chk5.Size = new System.Drawing.Size(96, 16);
			this.chk5.TabIndex = 12;
			this.chk5.Text = "Comments";
			// 
			// chkAll
			// 
			this.chkAll.Location = new System.Drawing.Point(7, 16);
			this.chkAll.Name = "chkAll";
			this.chkAll.Size = new System.Drawing.Size(96, 16);
			this.chkAll.TabIndex = 1;
			this.chkAll.Text = "All columns";
			// 
			// chk4
			// 
			this.chk4.Location = new System.Drawing.Point(7, 82);
			this.chk4.Name = "chk4";
			this.chk4.Size = new System.Drawing.Size(88, 16);
			this.chk4.TabIndex = 4;
			this.chk4.Text = "Label Note";
			// 
			// btnClose
			// 
			this.btnClose.Location = new System.Drawing.Point(160, 144);
			this.btnClose.Name = "btnClose";
			this.btnClose.TabIndex = 43;
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnReplaceAll
			// 
			this.btnReplaceAll.Location = new System.Drawing.Point(128, 40);
			this.btnReplaceAll.Name = "btnReplaceAll";
			this.btnReplaceAll.Size = new System.Drawing.Size(144, 23);
			this.btnReplaceAll.TabIndex = 42;
			this.btnReplaceAll.Text = "Replace all";
			this.btnReplaceAll.Click += new System.EventHandler(this.btnReplaceAll_Click);
			// 
			// btnReplaceAllMatch
			// 
			this.btnReplaceAllMatch.Enabled = false;
			this.btnReplaceAllMatch.Location = new System.Drawing.Point(128, 104);
			this.btnReplaceAllMatch.Name = "btnReplaceAllMatch";
			this.btnReplaceAllMatch.Size = new System.Drawing.Size(144, 23);
			this.btnReplaceAllMatch.TabIndex = 41;
			this.btnReplaceAllMatch.Text = "Replace all matching ";
			// 
			// btnFindNext
			// 
			this.btnFindNext.Enabled = false;
			this.btnFindNext.Location = new System.Drawing.Point(128, 72);
			this.btnFindNext.Name = "btnFindNext";
			this.btnFindNext.Size = new System.Drawing.Size(144, 23);
			this.btnFindNext.TabIndex = 40;
			this.btnFindNext.Text = "Find next match";
			// 
			// btnReplaceSelected
			// 
			this.btnReplaceSelected.Location = new System.Drawing.Point(128, 8);
			this.btnReplaceSelected.Name = "btnReplaceSelected";
			this.btnReplaceSelected.Size = new System.Drawing.Size(168, 23);
			this.btnReplaceSelected.TabIndex = 39;
			this.btnReplaceSelected.Text = "Replace Selected Row(s)";
			this.btnReplaceSelected.Click += new System.EventHandler(this.btnReplaceSelected_Click);
			// 
			// chk6
			// 
			this.chk6.Location = new System.Drawing.Point(8, 126);
			this.chk6.Name = "chk6";
			this.chk6.Size = new System.Drawing.Size(96, 16);
			this.chk6.TabIndex = 15;
			this.chk6.Text = "Location";
			// 
			// NewFromExistingInEditDlg
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(312, 174);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnReplaceAll);
			this.Controls.Add(this.btnReplaceAllMatch);
			this.Controls.Add(this.btnFindNext);
			this.Controls.Add(this.btnReplaceSelected);
			this.Controls.Add(this.groupBox1);
			this.Name = "NewFromExistingInEditDlg";
			this.Text = "NewFromExistingInEditDlg";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnReplaceSelected_Click(object sender, System.EventArgs e)
		{
			EditBatchEventArgs eva;
			eva = SetEventArgs();

			if (!VerifyReplaceColumns(eva))
				return;

			eva.ButtonName = "ReplaceSelected";
			if (ButtonClick != null)
				ButtonClick(this, eva);
		}

		private bool VerifyReplaceColumns(EditBatchEventArgs eva)
		{
			foreach (bool b in eva.ReplaceColumns)
			{
				if (b)
				{
					return true;
				}
			}
			MessageBox.Show(this, "Select a replacement column", "Select Replacement Column", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return false;
		}

		private void btnReplaceAll_Click(object sender, System.EventArgs e)
		{
			EditBatchEventArgs eva;
			eva = SetEventArgs();

			if (!VerifyReplaceColumns(eva))
				return;
			StringBuilder sb = new StringBuilder("You are about to replace 'ALL' rows for selected columns with changes.");
			sb.Append(Environment.NewLine);
			sb.Append("Press 'Yes' to continue.");
			if (MessageBox.Show(this, sb.ToString(), "Confirm To Continue", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
				return;
			eva.ButtonName = "ReplaceAll";
			if (ButtonClick != null)
				ButtonClick(this, eva);
		}

		private EditBatchEventArgs SetEventArgs()
		{
			EditBatchEventArgs e = new EditBatchEventArgs();

			e.ReplaceColumns[0] = chkAll.Checked;
			e.ReplaceColumns[1] = chk2.Checked;
			e.ReplaceColumns[2] = chk3.Checked;
			e.ReplaceColumns[3] = chk4.Checked;
			e.ReplaceColumns[4] = chk5.Checked;
			e.ReplaceColumns[5] = chk6.Checked;
			return e;
		}

	}
}
